using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Shapes;
namespace Microsoft.Phone.Controls
{
	[TemplatePart(Name = "InnerHintPanel", Type = typeof(Rectangle)), TemplatePart(Name = "OutterHintPanel", Type = typeof(Rectangle)), TemplatePart(Name = "OutterCover", Type = typeof(Grid)), TemplatePart(Name = "InfoPresenter", Type = typeof(ContentControl)), TemplateVisualState(Name = "Closed", GroupName = "SelectionEnabledStates"), TemplateVisualState(Name = "Exposed", GroupName = "SelectionEnabledStates"), TemplateVisualState(Name = "Opened", GroupName = "SelectionEnabledStates")]
	public class MultiselectItem : ContentControl
	{
		private const string SelectionEnabledStates = "SelectionEnabledStates";
		private const string Closed = "Closed";
		private const string Exposed = "Exposed";
		private const string Opened = "Opened";
		private const string SelectBox = "SelectBox";
		private const string OutterHintPanel = "OutterHintPanel";
		private const string InnerHintPanel = "InnerHintPanel";
		private const string OutterCover = "OutterCover";
		private const string InfoPresenter = "InfoPresenter";
		private const double _deltaLimitX = 0.0;
		private const double _deltaLimitY = 0.4;
		private Rectangle _outterHintPanel;
		private Rectangle _innerHintPanel;
		private Grid _outterCover;
		private ContentControl _infoPresenter;
		private MultiselectList _parent;
		private double _manipulationDeltaX;
		private double _manipulationDeltaY;
		internal bool _isBeingVirtualized;
		internal bool _canTriggerSelectionChanged = true;
		public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.Register("IsSelected", typeof(bool), typeof(MultiselectItem), new PropertyMetadata(false, new PropertyChangedCallback(MultiselectItem.OnIsSelectedPropertyChanged)));
		internal static readonly DependencyProperty StateProperty = DependencyProperty.Register("State", typeof(SelectionEnabledState), typeof(MultiselectItem), new PropertyMetadata(SelectionEnabledState.Closed, null));
		public static readonly DependencyProperty HintPanelHeightProperty = DependencyProperty.Register("HintPanelHeight", typeof(double), typeof(MultiselectItem), new PropertyMetadata(double.NaN, null));
		public static readonly DependencyProperty ContentInfoProperty = DependencyProperty.Register("ContentInfo", typeof(object), typeof(MultiselectItem), new PropertyMetadata(null, new PropertyChangedCallback(MultiselectItem.OnContentInfoPropertyChanged)));
		public static readonly DependencyProperty ContentInfoTemplateProperty = DependencyProperty.Register("ContentInfoTemplate", typeof(DataTemplate), typeof(MultiselectItem), new PropertyMetadata(null, new PropertyChangedCallback(MultiselectItem.OnContentInfoTemplatePropertyChanged)));
		public event RoutedEventHandler Selected;
		public event RoutedEventHandler Unselected;
		public bool IsSelected
		{
			get
			{
				return (bool)base.GetValue(MultiselectItem.IsSelectedProperty);
			}
			set
			{
				base.SetValue(MultiselectItem.IsSelectedProperty, value);
			}
		}
		internal SelectionEnabledState State
		{
			get
			{
				return (SelectionEnabledState)base.GetValue(MultiselectItem.StateProperty);
			}
			set
			{
				base.SetValue(MultiselectItem.StateProperty, value);
			}
		}
		public double HintPanelHeight
		{
			get
			{
				return (double)base.GetValue(MultiselectItem.HintPanelHeightProperty);
			}
			set
			{
				base.SetValue(MultiselectItem.HintPanelHeightProperty, value);
			}
		}
		public object ContentInfo
		{
			get
			{
				return base.GetValue(MultiselectItem.ContentInfoProperty);
			}
			set
			{
				base.SetValue(MultiselectItem.ContentInfoProperty, value);
			}
		}
		public DataTemplate ContentInfoTemplate
		{
			get
			{
				return (DataTemplate)base.GetValue(MultiselectItem.ContentInfoTemplateProperty);
			}
			set
			{
				base.SetValue(MultiselectItem.ContentInfoTemplateProperty, value);
			}
		}
		private static void OnIsSelectedPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
		{
			MultiselectItem multiselectItem = (MultiselectItem)obj;
			RoutedEventArgs e2 = new RoutedEventArgs();
			bool flag;
			if (flag = (bool)e.get_NewValue())
			{
				multiselectItem.OnSelected(e2);
			}
			else
			{
				multiselectItem.OnUnselected(e2);
			}
			if (multiselectItem._parent != null && !multiselectItem._isBeingVirtualized)
			{
				if (flag)
				{
					multiselectItem._parent.SelectedItems.Add(multiselectItem.get_Content());
					if (multiselectItem._canTriggerSelectionChanged)
					{
						multiselectItem._parent.OnSelectionChanged(new object[0], new object[]
						{
							multiselectItem.get_Content()
						});
						return;
					}
				}
				else
				{
					multiselectItem._parent.SelectedItems.Remove(multiselectItem.get_Content());
					if (multiselectItem._canTriggerSelectionChanged)
					{
						multiselectItem._parent.OnSelectionChanged(new object[]
						{
							multiselectItem.get_Content()
						}, new object[0]);
					}
				}
			}
		}
		private static void OnHintPanelHeightPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
		{
			MultiselectItem multiselectItem = (MultiselectItem)obj;
			if (multiselectItem._outterHintPanel != null)
			{
				if (double.IsNaN((double)e.get_NewValue()))
				{
					multiselectItem._outterHintPanel.set_VerticalAlignment(3);
				}
				else
				{
					multiselectItem._outterHintPanel.set_VerticalAlignment(0);
				}
			}
			if (multiselectItem._innerHintPanel != null)
			{
				if (double.IsNaN(multiselectItem.HintPanelHeight))
				{
					multiselectItem._innerHintPanel.set_VerticalAlignment(3);
					return;
				}
				multiselectItem._innerHintPanel.set_VerticalAlignment(0);
			}
		}
		private static void OnContentInfoPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
		{
			MultiselectItem multiselectItem = (MultiselectItem)obj;
			multiselectItem.OnContentInfoChanged(e.get_OldValue(), e.get_NewValue());
		}
		private static void OnContentInfoTemplatePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
		{
			MultiselectItem multiselectItem = (MultiselectItem)obj;
			DataTemplate oldContentInfoTemplate = e.get_OldValue() as DataTemplate;
			DataTemplate newContentInfoTemplate = e.get_NewValue() as DataTemplate;
			multiselectItem.OnContentInfoTemplateChanged(oldContentInfoTemplate, newContentInfoTemplate);
		}
		public override void OnApplyTemplate()
		{
			this._parent = ItemsControlExtensions.GetParentItemsControl<MultiselectList>(this);
			if (this._innerHintPanel != null)
			{
				this._innerHintPanel.remove_ManipulationStarted(new EventHandler<ManipulationStartedEventArgs>(this.HintPanel_ManipulationStarted));
				this._innerHintPanel.remove_ManipulationDelta(new EventHandler<ManipulationDeltaEventArgs>(this.HintPanel_ManipulationDelta));
				this._innerHintPanel.remove_ManipulationCompleted(new EventHandler<ManipulationCompletedEventArgs>(this.HintPanel_ManipulationCompleted));
			}
			if (this._outterHintPanel != null)
			{
				this._outterHintPanel.remove_ManipulationStarted(new EventHandler<ManipulationStartedEventArgs>(this.HintPanel_ManipulationStarted));
				this._outterHintPanel.remove_ManipulationDelta(new EventHandler<ManipulationDeltaEventArgs>(this.HintPanel_ManipulationDelta));
				this._outterHintPanel.remove_ManipulationCompleted(new EventHandler<ManipulationCompletedEventArgs>(this.HintPanel_ManipulationCompleted));
			}
			if (this._outterCover != null)
			{
				this._outterCover.remove_Tap(new EventHandler<GestureEventArgs>(this.Cover_Tap));
			}
			this._innerHintPanel = (base.GetTemplateChild("InnerHintPanel") as Rectangle);
			this._outterHintPanel = (base.GetTemplateChild("OutterHintPanel") as Rectangle);
			this._outterCover = (base.GetTemplateChild("OutterCover") as Grid);
			this._infoPresenter = (base.GetTemplateChild("InfoPresenter") as ContentControl);
			base.OnApplyTemplate();
			if (this._innerHintPanel != null)
			{
				this._innerHintPanel.add_ManipulationStarted(new EventHandler<ManipulationStartedEventArgs>(this.HintPanel_ManipulationStarted));
				this._innerHintPanel.add_ManipulationDelta(new EventHandler<ManipulationDeltaEventArgs>(this.HintPanel_ManipulationDelta));
				this._innerHintPanel.add_ManipulationCompleted(new EventHandler<ManipulationCompletedEventArgs>(this.HintPanel_ManipulationCompleted));
			}
			if (this._outterHintPanel != null)
			{
				this._outterHintPanel.add_ManipulationStarted(new EventHandler<ManipulationStartedEventArgs>(this.HintPanel_ManipulationStarted));
				this._outterHintPanel.add_ManipulationDelta(new EventHandler<ManipulationDeltaEventArgs>(this.HintPanel_ManipulationDelta));
				this._outterHintPanel.add_ManipulationCompleted(new EventHandler<ManipulationCompletedEventArgs>(this.HintPanel_ManipulationCompleted));
			}
			if (this._outterCover != null)
			{
				this._outterCover.add_Tap(new EventHandler<GestureEventArgs>(this.Cover_Tap));
			}
			if (this.ContentInfo == null && this._parent != null && this._parent.ItemInfoTemplate != null)
			{
				this._infoPresenter.set_ContentTemplate(this._parent.ItemInfoTemplate);
				Binding binding = new Binding();
				base.SetBinding(MultiselectItem.ContentInfoProperty, binding);
			}
			if (this._outterHintPanel != null)
			{
				if (double.IsNaN(this.HintPanelHeight))
				{
					this._outterHintPanel.set_VerticalAlignment(3);
				}
				else
				{
					this._outterHintPanel.set_VerticalAlignment(0);
				}
			}
			if (this._innerHintPanel != null)
			{
				if (double.IsNaN(this.HintPanelHeight))
				{
					this._innerHintPanel.set_VerticalAlignment(3);
				}
				else
				{
					this._innerHintPanel.set_VerticalAlignment(0);
				}
			}
			this.UpdateVisualState(false);
		}
		public MultiselectItem()
		{
			base.set_DefaultStyleKey(typeof(MultiselectItem));
		}
		internal void UpdateVisualState(bool useTransitions)
		{
			string text;
			switch (this.State)
			{
			case SelectionEnabledState.Closed:
				text = "Closed";
				break;
			case SelectionEnabledState.Exposed:
				text = "Exposed";
				break;
			case SelectionEnabledState.Opened:
				text = "Opened";
				break;
			default:
				text = "Closed";
				break;
			}
			VisualStateManager.GoToState(this, text, useTransitions);
		}
		private void RaiseEvent(RoutedEventHandler handler, RoutedEventArgs args)
		{
			if (handler != null)
			{
				handler.Invoke(this, args);
			}
		}
		protected virtual void OnSelected(RoutedEventArgs e)
		{
			if (this._parent == null)
			{
				this.State = SelectionEnabledState.Opened;
				this.UpdateVisualState(true);
			}
			this.RaiseEvent(this.Selected, e);
		}
		protected virtual void OnUnselected(RoutedEventArgs e)
		{
			if (this._parent == null)
			{
				this.State = SelectionEnabledState.Closed;
				this.UpdateVisualState(true);
			}
			this.RaiseEvent(this.Unselected, e);
		}
		protected virtual void OnContentInfoChanged(object oldContentInfo, object newContentInfo)
		{
		}
		protected virtual void OnContentInfoTemplateChanged(DataTemplate oldContentInfoTemplate, DataTemplate newContentInfoTemplate)
		{
		}
		private void HintPanel_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
		{
			this.State = SelectionEnabledState.Exposed;
			this.UpdateVisualState(true);
		}
		private void HintPanel_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
		{
			this._manipulationDeltaX = e.get_DeltaManipulation().get_Translation().get_X();
			this._manipulationDeltaY = e.get_DeltaManipulation().get_Translation().get_Y();
			if (this._manipulationDeltaX < 0.0)
			{
				this._manipulationDeltaX *= -1.0;
			}
			if (this._manipulationDeltaY < 0.0)
			{
				this._manipulationDeltaY *= -1.0;
			}
			if (this._manipulationDeltaX > 0.0 || this._manipulationDeltaY >= 0.4)
			{
				this.State = SelectionEnabledState.Closed;
				this.UpdateVisualState(true);
			}
		}
		private void HintPanel_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
		{
			if (this._manipulationDeltaX == 0.0 && this._manipulationDeltaY < 0.4)
			{
				this.IsSelected = true;
			}
			this._manipulationDeltaX = 0.0;
			this._manipulationDeltaY = 0.0;
		}
		private void Cover_Tap(object sender, GestureEventArgs e)
		{
			this.IsSelected = !this.IsSelected;
		}
	}
}
